package controller;

import Request.*;
import Response.Follow;
import Util.FileUtil;
import WebSocket.WebsocketEndPoint;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import modal.RoomMaster;
import modal.User;
import modal.UserApplication;
import modal.UserVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.ActivityService;
import service.PersonManageService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/person")
public class PesonManageController {


    @Autowired
    PersonManageService personManageService;
    @Autowired
    ActivityService activityService;

    /**
     * 更新用户信息
     *
     * @param user     用户信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateInfo.do")
    @ResponseBody
    public String updateInfo(User user, HttpServletRequest request, HttpServletResponse response) {
        System.err.println(user);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return personManageService.updateInfo(user);
    }

    /**
     * 提交申请
     *
     * @param application 申请
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/application.do")
    @ResponseBody
    public String application(UserApplication application, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(application);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return personManageService.submitApplication(application);
    }

    /**
     * 查询关注列表
     *
     * @param name     根据用户姓名查询
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/follow.do")
    @ResponseBody
    public String follow(@RequestParam("username") String name, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(name);
        List<Follow> list = personManageService.getfollows(name);
        System.out.println(JSON.toJSONString(list, SerializerFeature.WriteMapNullValue));
        return JSON.toJSONString(list, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 取消关注
     *
     * @param result
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/unfollow.do")
    @ResponseBody
    public String unfollow(UnFollowResult result, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(result);
        return personManageService.deletefollow(result.getUsername(), result.getRoomid());
    }

    /**
     * 重置密码
     *
     * @param user
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/resetPassowrd.do")
    @ResponseBody
    public String resetPassword(User user, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(user);
        return personManageService.updateInfo(user);
    }

    /**
     * 获取直播间信息
     *
     * @param username 用户名
     * @return
     */
    @RequestMapping(value = "/getMyRoom.do")
    @ResponseBody
    public String getRoom(@RequestParam(value = "username") String username, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        RoomMaster room = personManageService.getMyRoom(username);
        System.out.print(room);
        return JSON.toJSONString(room, SerializerFeature.WriteMapNullValue);

    }

    /**
     * 更新直播间信息
     *
     * @param room
     * @return
     */
    @RequestMapping(value = "/updateRoom.do")
    @ResponseBody
    public String updateroom(RoomMaster room, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(room);
        return personManageService.updateRoomMaster(room);
    }

    /**
     * 上传图片
     *
     * @param pictureRequest 图片的信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/uploadpicture.do", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public String uploadPicture(@RequestBody UploadPictureRequest pictureRequest, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(pictureRequest);
        FileUtil fileUtil = new FileUtil(request);
        return fileUtil.uploadImg(pictureRequest.getPicturestr(), pictureRequest.getUsername());
    }

    /**
     * 上传视频
     *
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/uploadvideo.do")
    @ResponseBody
    public String uploadvideo(@RequestParam("file") MultipartFile file,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        System.out.println(file);
        FileUtil fileUtil = new FileUtil(file, request);
        return fileUtil.uploadVideo();
    }

    /**
     * 更新视频信息
     *
     * @param videoRequest
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/addvideo.do")
    @ResponseBody
    public String addvideo(VideoRequest videoRequest, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(videoRequest);
        return personManageService.addVideo(videoRequest);
    }

    /*打开直播 1:打开 0关闭*/
    @RequestMapping(value = "/Live.do")
    @ResponseBody
    public String openLive(LiveRequest liveRequest, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(liveRequest);
        return personManageService.openLive(liveRequest);
    }

    /**
     * 获取用户上传的视频信息
     *
     * @param username 用户名
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getMyVideo.do")
    @ResponseBody
    public String getmyvide(@RequestParam(value = "username") String username, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(username);
        List<UserVideo> list = activityService.getListVideo(username);
        return JSON.toJSONString(list, SerializerFeature.WriteMapNullValue);
    }

    @RequestMapping("/getRoomInfo")
    @ResponseBody
    public RoomMaster getRoomInfo(@RequestParam(value = "roomid") String Roomid) {
        return personManageService.getRoomInformationByRoomid(Roomid);
    }

    @RequestMapping(value = "/activityFollow.do")
    @ResponseBody
    public String test6(FollowRequest followRequest, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(followRequest);
        return activityService.followEvent(followRequest.getUsername(), followRequest.getRoomid());
    }

    @RequestMapping(value = "/activityUnFollow.do")
    @ResponseBody
    public String test7(FollowRequest followRequest, HttpServletRequest request, HttpServletResponse response) {
        return activityService.unfollowEvent(followRequest.getUsername(), followRequest.getRoomid());
    }

    @RequestMapping(value = "/hasFollow.do")
    @ResponseBody
    public String hasFollow(FollowRequest followRequest , HttpServletRequest request, HttpServletResponse response)
    {
        return activityService.hasFollow(followRequest.getUsername(),followRequest.getRoomid());
    }

    @RequestMapping(value = "/getNum.do")
    @ResponseBody
    public String getNum(@RequestParam(value = "roomid") String roomid)
    {

        return WebsocketEndPoint.getRoomNumber(roomid);
    }
}
